package com.draemoncode.weixindb.event;

import com.draemoncode.weixindb.constant.MessagePackageTypeConstant;
import com.draemoncode.weixindb.constant.ShangZhuangFieldConstant;
import com.draemoncode.weixindb.conventer.CMessagePackageConventer;
import com.draemoncode.weixindb.domain.B_Pod;
import com.draemoncode.weixindb.server.CMessagePackage;
import com.draemoncode.weixindb.server.IEvent;
import com.draemoncode.weixindb.service.BusiService;
import com.draemoncode.weixindb.util.ValueUtil;
import com.draemoncode.weixindb.websocket.UIMessage;
import com.draemoncode.weixindb.websocket.UISession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 第1步 上庄
 * @author yangz
 * @date 2017/4/9
 *
 */
@Component
public class ShangZhuangEvent implements IEvent<B_Pod, Object> {

    @Autowired
    private BusiService busiService;



    @Override
    public Boolean shoudProcess(CMessagePackage cMessagePackage) {
        if(MessagePackageTypeConstant.REQUEST_SHANG_ZHUANG.equals(cMessagePackage.getType())){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public B_Pod busiExec(CMessagePackage cMessagePackage) {
        String username = ValueUtil.toString(cMessagePackage.getMsg().get(ShangZhuangFieldConstant.REQUEST_USER_NAME));
        BigDecimal money = ValueUtil.toBigDecimal(cMessagePackage.getMsg().get(ShangZhuangFieldConstant.REQUEST_MONEY));
        B_Pod pod = busiService.shangzhuang(username, money);
        return pod;
    }

    @Override
    public Object uiExec(CMessagePackage cMessagePackage, B_Pod busiResult) {
        UIMessage uiMessage = CMessagePackageConventer.toUIMessage(cMessagePackage);
        uiMessage.getMsg().clear();
        uiMessage.getMsg().put(ShangZhuangFieldConstant.UI_ZHUANG_NAME, busiResult.getZhuang_name());
        uiMessage.getMsg().put(ShangZhuangFieldConstant.UI_ZHUANG_MONEY, ValueUtil.formatMoney(busiResult.getZhuang_money()));
        uiMessage.setPodId(busiResult.getId());
        UISession.send(uiMessage);
        return null;
    }

    @Override
    public CMessagePackage buildResponse(CMessagePackage cMessagePackage, B_Pod busiResult, Object uiResult) {
        CMessagePackage response = new CMessagePackage(MessagePackageTypeConstant.RESPONSE_RECEIVE);
        response.setSyncid(busiResult.getId());
        return response;
    }


}
